package com.example.demo.config;

import com.example.demo.controller.TestWebSocketController;
import com.example.demo.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket //启用这个websocket配置
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private TestWebSocketController testWebSocketController;

    @Autowired
    private WebSocketController webSocketController;

    //通过这个方法,把刚才创建好的Handler类注册到具体的路径上
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //将路径和WebSocketController对象关联.只有访问这个路径的时候才会触发
        //registry.addHandler(testWebSocketController,"/test");
        //registry.addHandler(webSocketController,"/message")
        registry.addHandler(webSocketController,"/WebSocketMessage")
                //通过注册特定的拦截器,就可以把用户在HttpSession中添加的键值对拷贝到WebSocketSession中一份
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
